package logic

import (
	"context"
	"fmt"
	"shopping-mall/models/mysql"

	"shopping-mall/shoppingrpc/internal/svc"
	"shopping-mall/shoppingrpc/shoppingrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductsAddLogic {
	return &ProductsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductsAddLogic) ProductsAdd(in *shoppingrpc.ProductsAddRequest) (*shoppingrpc.ProductsAddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		Img:         in.Img,
		Video:       in.Video,
		Name:        in.Name,
		Description: in.Description,
		Price:       float64(in.Price),
	}
	err := mysql.Db.Debug().Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}

	return &shoppingrpc.ProductsAddResponse{Id: int64(product.ID)}, nil
}
